﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;

namespace FasterWPF
{
    public static class TreeViewExt
    {
        public static TreeView SetBackgroundColor(this TreeView chain, Brush brush)
        {
            Brush newBrush = (brush);

            #region Change TreeView Style

            Style newStyle = null;
            object resourceKey = null;
            foreach (DictionaryEntry o in chain.Resources.MergedDictionaries[0])
            {
                if ((o.Key is Type) && (((Type)o.Key).Name == "TreeView"))
                {
                    resourceKey = o.Key;
                    newStyle = new Style(typeof(TreeView), (o.Value as Style));
                }
            }

            Setter setter = new Setter();
            setter.Property = TreeView.BackgroundProperty;
            setter.Value = newBrush;
            newStyle.Setters.Add(setter);
            chain.Resources.MergedDictionaries[0][resourceKey] = newStyle;

            #endregion

            return chain;
        }

        public static TreeView SetTreeViewItemBackgroundColor(this TreeView chain, Brush backgroundColor)
        {           
            #region Change TreeViewItem Style

            Style newStyle2 = null;
            object resourceKey2 = null;
            foreach (DictionaryEntry o2 in chain.Resources.MergedDictionaries[0])
            {
                if ((o2.Key is Type) && (((Type)o2.Key).Name == "TreeViewItem"))
                {
                    resourceKey2 = o2.Key;
                    newStyle2 = new Style(typeof(TreeViewItem), (o2.Value as Style));
                }
            }

            Setter setter2 = new Setter();
            setter2.Property = TreeViewItem.BackgroundProperty;
            setter2.Value = backgroundColor;
            newStyle2.Setters.Add(setter2);

            chain.Resources.MergedDictionaries[0][resourceKey2] = newStyle2;

            #endregion

            return chain;
        }

        public static TreeView SetTreeViewItemForegroundColor(this TreeView chain, Brush foregroundColor)
        {
            #region Change TreeViewItem Style

            Style newStyle2 = null;
            object resourceKey2 = null;
            foreach (DictionaryEntry o2 in chain.Resources.MergedDictionaries[0])
            {
                if ((o2.Key is Type) && (((Type)o2.Key).Name == "TreeViewItem"))
                {
                    resourceKey2 = o2.Key;
                    newStyle2 = new Style(typeof(TreeViewItem), (o2.Value as Style));
                }
            }

            Setter setter2 = new Setter();
            setter2.Property = TreeViewItem.ForegroundProperty;
            setter2.Value = foregroundColor;
            newStyle2.Setters.Add(setter2);

            chain.Resources.MergedDictionaries[0][resourceKey2] = newStyle2;

            #endregion

            return chain;
        }

        public static TreeView SetArrowColors(this TreeView chain, string backgroundColor, string border, string mouseOverColor, string mouseOverBorder, string expandedColor, string expandedBorder, int borderWidth, int borderHeight)
        {          
            //Path myPath = new Path();
            //myPath.Stroke = System.Windows.Media.Brushes.Yellow;
            //myPath.Fill = System.Windows.Media.Brushes.Green;
            //myPath.StrokeThickness = 4;
            //myPath.HorizontalAlignment = HorizontalAlignment.Left;
            //myPath.VerticalAlignment = VerticalAlignment.Center;
            //EllipseGeometry myEllipseGeometry = new EllipseGeometry();
            //myEllipseGeometry.Center = new System.Windows.Point(50, 50);
            //myEllipseGeometry.RadiusX = 25;
            //myEllipseGeometry.RadiusY = 25;
            //myPath.Data = myEllipseGeometry;

            #region Change TreeViewItem Style including ToggleButton Control Template

            Style newStyle2 = null;
            object resourceKey2 = null;
            foreach (DictionaryEntry o2 in chain.Resources.MergedDictionaries[0])
            {
                if ((o2.Key is Type) && (((Type)o2.Key).Name=="TreeViewItem"))
                {
                    resourceKey2 = o2.Key;
                    newStyle2 = new Style(typeof(TreeViewItem), (o2.Value as Style));
                }               
            }
                       
            Setter setter3 = new Setter();
            setter3.Property = TreeViewItem.TemplateProperty;

            string xamlTreeViewItemTemplate = "<ControlTemplate TargetType=\"{x:Type TreeViewItem}\"><ControlTemplate.Resources><PathGeometry x:Key=\"TreeArrow\"><PathGeometry.Figures><PathFigureCollection><PathFigure IsFilled=\"True\" StartPoint=\"0 0\" IsClosed=\"True\"><PathFigure.Segments><PathSegmentCollection><LineSegment Point=\"0 6\"/><LineSegment Point=\"6 0\"/></PathSegmentCollection></PathFigure.Segments></PathFigure></PathFigureCollection></PathGeometry.Figures></PathGeometry><Style x:Key=\"ExpandCollapseToggleStyle3\" TargetType=\"{x:Type ToggleButton}\"><Setter Property=\"Focusable\" Value=\"False\"/><Setter Property=\"Width\" Value=\"16\"/><Setter Property=\"Height\" Value=\"16\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Width=\""+borderWidth+"\" Height=\""+borderHeight+"\" Background=\"Transparent\"  Padding=\"5,5,5,5\"> <Path x:Name=\"ExpandPath\" Fill=\""+backgroundColor+"\"  Stroke=\""+border+"\" Data=\"{StaticResource TreeArrow}\">  <Path.RenderTransform> <RotateTransform Angle=\"135\" CenterX=\"3\" CenterY=\"3\"/>  </Path.RenderTransform> </Path></Border><ControlTemplate.Triggers>  <Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\""+mouseOverBorder+"\"/> <Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\""+mouseOverColor+"\"/></Trigger><Trigger Property=\"IsChecked\"  Value=\"True\"> <Setter TargetName=\"ExpandPath\"  Property=\"RenderTransform\"> <Setter.Value><RotateTransform Angle=\"180\" CenterX=\"3\" CenterY=\"3\"/> </Setter.Value></Setter><Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\""+expandedColor+"\"/> <Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\""+expandedBorder+"\"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate></Setter.Value></Setter></Style></ControlTemplate.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition MinWidth=\"19\" Width=\"Auto\"/><ColumnDefinition Width=\"Auto\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height=\"Auto\"/><RowDefinition/></Grid.RowDefinitions><ToggleButton x:Name=\"Expander\" Style=\"{StaticResource ExpandCollapseToggleStyle3}\" IsChecked=\"{Binding Path=IsExpanded,RelativeSource={RelativeSource TemplatedParent}}\" ClickMode=\"Press\"/><Border Name=\"Bd\" Grid.Column=\"1\" Background=\"{TemplateBinding Background}\" BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\" Padding=\"{TemplateBinding Padding}\" SnapsToDevicePixels=\"true\"><ContentPresenter x:Name=\"PART_Header\" ContentSource=\"Header\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/></Border><ItemsPresenter x:Name=\"ItemsHost\" Grid.Row=\"1\" Grid.Column=\"1\" Grid.ColumnSpan=\"2\"/></Grid><ControlTemplate.Triggers><Trigger Property=\"IsExpanded\" Value=\"false\"><Setter TargetName=\"ItemsHost\" Property=\"Visibility\" Value=\"Collapsed\"/></Trigger><Trigger Property=\"HasItems\" Value=\"false\"><Setter TargetName=\"Expander\" Property=\"Visibility\" Value=\"Hidden\"/></Trigger><Trigger Property=\"IsSelected\" Value=\"true\"><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"{DynamicResource {x:Static SystemColors.HighlightBrushKey}}\"/><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}\"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"IsSelectionActive\" Value=\"false\"/></MultiTrigger.Conditions><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"{DynamicResource {x:Static SystemColors.ControlBrushKey}}\"/><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}\"/></MultiTrigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr3 = new MemoryStream(Encoding.ASCII.GetBytes(xamlTreeViewItemTemplate));
            ParserContext pc3 = new ParserContext();
            pc3.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc3.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            ControlTemplate treItemTemplate = (ControlTemplate)XamlReader.Load(sr3, pc3);
            setter3.Value = treItemTemplate;
            newStyle2.Setters.Add(setter3);

            chain.Resources.MergedDictionaries[0][resourceKey2] = newStyle2;

            #endregion

            return chain;
        }

        public static TreeView SetArrowColors(this TreeView chain, string backgroundColor, string border, string mouseOverColor, string mouseOverBorder, string expandedColor, string expandedBorder, int borderWidth, int borderHeight,
            string pressedBackground, string pressedForeground, string selectedBackground, string selectedForeground
            )
        {
            #region Change TreeViewItem Style including ToggleButton Control Template

            Style newStyle2 = null;
            object resourceKey2 = null;
            foreach (DictionaryEntry o2 in chain.Resources.MergedDictionaries[0])
            {
                if ((o2.Key is Type) && (((Type)o2.Key).Name == "TreeViewItem"))
                {
                    resourceKey2 = o2.Key;
                    newStyle2 = new Style(typeof(TreeViewItem), (o2.Value as Style));
                }
            }

            Setter setter3 = new Setter();
            setter3.Property = TreeViewItem.TemplateProperty;

            string xamlTreeViewItemTemplate = "<ControlTemplate TargetType=\"{x:Type TreeViewItem}\"><ControlTemplate.Resources><PathGeometry x:Key=\"TreeArrow\"><PathGeometry.Figures><PathFigureCollection><PathFigure IsFilled=\"True\" StartPoint=\"0 0\" IsClosed=\"True\"><PathFigure.Segments><PathSegmentCollection><LineSegment Point=\"0 6\"/><LineSegment Point=\"6 0\"/></PathSegmentCollection></PathFigure.Segments></PathFigure></PathFigureCollection></PathGeometry.Figures></PathGeometry><Style x:Key=\"ExpandCollapseToggleStyle3\" TargetType=\"{x:Type ToggleButton}\"><Setter Property=\"Focusable\" Value=\"False\"/><Setter Property=\"Width\" Value=\"16\"/><Setter Property=\"Height\" Value=\"16\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Width=\"" + borderWidth + "\" Height=\"" + borderHeight + "\" Background=\"Transparent\"  Padding=\"5,5,5,5\"> <Path x:Name=\"ExpandPath\" Fill=\"" + backgroundColor + "\"  Stroke=\"" + border + "\" Data=\"{StaticResource TreeArrow}\">  <Path.RenderTransform> <RotateTransform Angle=\"135\" CenterX=\"3\" CenterY=\"3\"/>  </Path.RenderTransform> </Path></Border><ControlTemplate.Triggers>  <Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\"" + mouseOverBorder + "\"/> <Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\"" + mouseOverColor + "\"/></Trigger><Trigger Property=\"IsChecked\"  Value=\"True\"> <Setter TargetName=\"ExpandPath\"  Property=\"RenderTransform\"> <Setter.Value><RotateTransform Angle=\"180\" CenterX=\"3\" CenterY=\"3\"/> </Setter.Value></Setter><Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\"" + expandedColor + "\"/> <Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\"" + expandedBorder + "\"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate></Setter.Value></Setter></Style></ControlTemplate.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition MinWidth=\"19\" Width=\"Auto\"/><ColumnDefinition Width=\"Auto\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height=\"Auto\"/><RowDefinition/></Grid.RowDefinitions><ToggleButton x:Name=\"Expander\" Style=\"{StaticResource ExpandCollapseToggleStyle3}\" IsChecked=\"{Binding Path=IsExpanded,RelativeSource={RelativeSource TemplatedParent}}\" ClickMode=\"Press\"/><Border Name=\"Bd\" Grid.Column=\"1\" Background=\"{TemplateBinding Background}\" BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\" Padding=\"{TemplateBinding Padding}\" SnapsToDevicePixels=\"true\"><ContentPresenter x:Name=\"PART_Header\" ContentSource=\"Header\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/></Border><ItemsPresenter x:Name=\"ItemsHost\" Grid.Row=\"1\" Grid.Column=\"1\" Grid.ColumnSpan=\"2\"/></Grid><ControlTemplate.Triggers><Trigger Property=\"IsExpanded\" Value=\"false\"><Setter TargetName=\"ItemsHost\" Property=\"Visibility\" Value=\"Collapsed\"/></Trigger><Trigger Property=\"HasItems\" Value=\"false\"><Setter TargetName=\"Expander\" Property=\"Visibility\" Value=\"Hidden\"/></Trigger><Trigger Property=\"IsSelected\" Value=\"true\"><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + selectedBackground + "\"/><Setter Property=\"Foreground\" Value=\"" + selectedForeground + "\"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"IsSelectionActive\" Value=\"false\"/></MultiTrigger.Conditions><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + pressedBackground + "\"/><Setter Property=\"Foreground\" Value=\"" + pressedForeground + "\"/></MultiTrigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr3 = new MemoryStream(Encoding.ASCII.GetBytes(xamlTreeViewItemTemplate));
            ParserContext pc3 = new ParserContext();
            pc3.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc3.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            ControlTemplate treItemTemplate = (ControlTemplate)XamlReader.Load(sr3, pc3);
            setter3.Value = treItemTemplate;
            newStyle2.Setters.Add(setter3);

            chain.Resources.MergedDictionaries[0][resourceKey2] = newStyle2;

            #endregion

            return chain;
        }

        public static TreeView SetArrowColors(this TreeView chain, string backgroundColor, string border, string mouseOverColor, string mouseOverBorder, string expandedColor, string expandedBorder, int borderWidth, int borderHeight,
           string pressedBackground, string pressedForeground, string selectedBackground, string selectedForeground,
           string pathGeometryXaml)
        {
            #region Change TreeViewItem Style including ToggleButton Control Template

            Style newStyle2 = null;
            object resourceKey2 = null;
            foreach (DictionaryEntry o2 in chain.Resources.MergedDictionaries[0])
            {
                if ((o2.Key is Type) && (((Type)o2.Key).Name == "TreeViewItem"))
                {
                    resourceKey2 = o2.Key;
                    newStyle2 = new Style(typeof(TreeViewItem), (o2.Value as Style));
                }
            }

            Setter setter3 = new Setter();
            setter3.Property = TreeViewItem.TemplateProperty;

            string xamlTreeViewItemTemplate = "<ControlTemplate TargetType=\"{x:Type TreeViewItem}\"><ControlTemplate.Resources><PathGeometry x:Key=\"TreeArrow\">"+pathGeometryXaml+"<Style x:Key=\"ExpandCollapseToggleStyle3\" TargetType=\"{x:Type ToggleButton}\"><Setter Property=\"Focusable\" Value=\"False\"/><Setter Property=\"Width\" Value=\"16\"/><Setter Property=\"Height\" Value=\"16\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Width=\"" + borderWidth + "\" Height=\"" + borderHeight + "\" Background=\"Transparent\"  Padding=\"5,5,5,5\"> <Path x:Name=\"ExpandPath\" Fill=\"" + backgroundColor + "\"  Stroke=\"" + border + "\" Data=\"{StaticResource TreeArrow}\">  <Path.RenderTransform> <RotateTransform Angle=\"135\" CenterX=\"3\" CenterY=\"3\"/>  </Path.RenderTransform> </Path></Border><ControlTemplate.Triggers>  <Trigger Property=\"IsMouseOver\" Value=\"True\"><Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\"" + mouseOverBorder + "\"/> <Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\"" + mouseOverColor + "\"/></Trigger><Trigger Property=\"IsChecked\"  Value=\"True\"> <Setter TargetName=\"ExpandPath\"  Property=\"RenderTransform\"> <Setter.Value><RotateTransform Angle=\"180\" CenterX=\"3\" CenterY=\"3\"/> </Setter.Value></Setter><Setter TargetName=\"ExpandPath\" Property=\"Fill\" Value=\"" + expandedColor + "\"/> <Setter TargetName=\"ExpandPath\" Property=\"Stroke\" Value=\"" + expandedBorder + "\"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate></Setter.Value></Setter></Style></ControlTemplate.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition MinWidth=\"19\" Width=\"Auto\"/><ColumnDefinition Width=\"Auto\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height=\"Auto\"/><RowDefinition/></Grid.RowDefinitions><ToggleButton x:Name=\"Expander\" Style=\"{StaticResource ExpandCollapseToggleStyle3}\" IsChecked=\"{Binding Path=IsExpanded,RelativeSource={RelativeSource TemplatedParent}}\" ClickMode=\"Press\"/><Border Name=\"Bd\" Grid.Column=\"1\" Background=\"{TemplateBinding Background}\" BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\" Padding=\"{TemplateBinding Padding}\" SnapsToDevicePixels=\"true\"><ContentPresenter x:Name=\"PART_Header\" ContentSource=\"Header\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\"/></Border><ItemsPresenter x:Name=\"ItemsHost\" Grid.Row=\"1\" Grid.Column=\"1\" Grid.ColumnSpan=\"2\"/></Grid><ControlTemplate.Triggers><Trigger Property=\"IsExpanded\" Value=\"false\"><Setter TargetName=\"ItemsHost\" Property=\"Visibility\" Value=\"Collapsed\"/></Trigger><Trigger Property=\"HasItems\" Value=\"false\"><Setter TargetName=\"Expander\" Property=\"Visibility\" Value=\"Hidden\"/></Trigger><Trigger Property=\"IsSelected\" Value=\"true\"><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + selectedBackground + "\"/><Setter Property=\"Foreground\" Value=\"" + selectedForeground + "\"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"IsSelectionActive\" Value=\"false\"/></MultiTrigger.Conditions><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + pressedBackground + "\"/><Setter Property=\"Foreground\" Value=\"" + pressedForeground + "\"/></MultiTrigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr3 = new MemoryStream(Encoding.ASCII.GetBytes(xamlTreeViewItemTemplate));
            ParserContext pc3 = new ParserContext();
            pc3.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc3.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            ControlTemplate treItemTemplate = (ControlTemplate)XamlReader.Load(sr3, pc3);
            setter3.Value = treItemTemplate;
            newStyle2.Setters.Add(setter3);

            chain.Resources.MergedDictionaries[0][resourceKey2] = newStyle2;

            #endregion

            return chain;
        }

        public static TreeView Initialize(this TreeView source, ContainerType containerType)
        {  
               source.Initialize(double.NaN, double.NaN, containerType);
               return source;
        }


        public static TreeView Initialize(this TreeView source, ContainerType containerType, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, double.NaN, double.NaN, containerType);
        }

        public static TreeView Initialize(this TreeView source, double width, double height, ContainerType containerType, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, width, height, containerType);
        }

        public static TreeView Initialize(this TreeView source, double width, double height, ContainerType containerType)
        {            
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<TreeView>(source);
            source.SetIComposite<TreeView>(icomp);

            //STORE SETTING BUILD CHAIN AND GET CHAIN?!?  Put these in a Composite implements an interface
            TreeViewParent tvbsr = new TreeViewParent();
            source.StoreSetting<TreeView>("IParent", tvbsr);
            source.StoreSetting<TreeView>("IScrollViewerStrategy", tvbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<TreeView>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            return source;
        }

    }//end of class
}//end of namespace
